package com.woniuxy.woniuticket.ordermodule.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuticket.moviemodule.dao.CinemaMoviesDAO;
import com.woniuxy.woniuticket.moviemodule.service.MovieinfoService;
import com.woniuxy.woniuticket.ordermodule.service.CommentService;
import com.woniuxy.woniuticket.pojo.CinemaCommentReply;
import com.woniuxy.woniuticket.pojo.CinemaMovies;
import com.woniuxy.woniuticket.pojo.MoviesComment;
import com.woniuxy.woniuticket.pojo.UsersInfo;
import com.woniuxy.woniuticket.resultcommon.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Wu xianbo on 2019/6/4
 */

@Controller
@RequestMapping("/comment")
public class MovieCommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private MovieinfoService movieinfoService;


    //通过电影id获取影片信息。影片评论之前需要做的事情
    @RequestMapping("/movieinfo")
    public String getMovieInfoBymovieId(Integer movieid,String ordernum,Model model){
        System.out.println("根据电影id获取电影信息"+movieid+"订单id是："+ordernum);
        CinemaMovies movieinfo = movieinfoService.findMovieByMovieId(movieid);
        //存放影片信息
        model.addAttribute("movieinfo",movieinfo);
        //存放影片id
        model.addAttribute("movieId",movieid);
        //存放订单号(需要根据订单号将当前订单该为已评价)
        model.addAttribute("ordernum",ordernum);
        return "movieshow/moviecomment";
    }

    //添加评论.消费者评论电影,需要获取电影id和用户id以及订单号
    @RequestMapping("/addComment")
    @ResponseBody
    public JsonResult addComment(MoviesComment moviesComment, String orderNum, HttpSession session) {
        System.out.println(moviesComment+"订单号是："+orderNum);
        //获取用户userinfoid.
        Object userObj = session.getAttribute("user");
        Integer userInfoId = 0;
        if(userObj!=null){
            UsersInfo usersInfo = (UsersInfo) userObj;      //转化对象
            userInfoId = usersInfo.getId();
        }
        moviesComment.setUserId(userInfoId);
        try {
            commentService.addComment(moviesComment,orderNum);  //插入评论同时将用户待评价订单标记为已评价
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("评论失败");
        }
        return JsonResult.ok();
    }

    //根据用户id查询所有评论
    @RequestMapping("findComentByUid")
    public String findAllCommentByUid(Model model,HttpSession session){
        //从session中获取当前用户的userinfoid
        Object userObj = session.getAttribute("user");
        Integer userInfoId = 0;
        UsersInfo usersInfo = null;
        if(userObj!=null){
             usersInfo = (UsersInfo) userObj;      //转化对象
            userInfoId = usersInfo.getId();
        }
        List<MoviesComment> moviesComments = commentService.findAllByUserId(userInfoId);
        //在modle中存放当前用户的评论
        model.addAttribute("myComments",moviesComments);
        //在model中存放当前用户的账号(或电话号码)
        String tel = usersInfo.getTelnumber();
        model.addAttribute("usertel",tel);
        return "/movieshow/usercomments";
    }

    //商家查询所有评论
    @RequestMapping("/findAllComments")
    @ResponseBody
    public Map<String,Object> findAllComments(@RequestParam(value = "page", defaultValue = "1", required = false) Integer cpage,
                                               @RequestParam(value = "limit", defaultValue = "10", required = false) Integer psize,
                                               String movieName) {
        System.out.println("查询所有评论"+movieName);
        List<MoviesComment> moviesComments = commentService.findAllByCinema(cpage, psize,movieName);
        PageInfo info = new PageInfo(moviesComments);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("data", moviesComments);
        resultMap.put("code", "0");
        resultMap.put("msg", "返回数据");
        resultMap.put("count", info.getTotal());
        return resultMap;
    }

    //根据电影id查询评论以及电影的详细信息(还需要封装电影的信息一起返回页面.此方法已被赵老师的controller取代)
    @RequestMapping("/findCommentsByMovieId")
    public String findCommentsByMovieId(Integer movieId, Model model) {
        System.out.println("电影id是：" + movieId);
        List<MoviesComment> moviesComments = commentService.findCommentsByMovieId(movieId);
        //存储对应电影的评论信息
        model.addAttribute("moviesComments", moviesComments);
        //存储当前的电影信息，在页面需要影藏电影id
        //TODO
        //获取当前用户信息(从shiro或者session中获取),并存入模型中
        model.addAttribute("loginstatus","未登录");
        return "/movieshow/details";
    }

    //其他用户回复某一条评论(需要评论id、用户id、评论内容)
    @RequestMapping("/replyComment")
    public String replyComment(CinemaCommentReply cinemaCommentReply,String movieId,HttpSession session) {
        System.out.println("评论"+movieId);
        //获取当前登录的用户id
        Object userObj = session.getAttribute("user");
        Integer userInfoId = 0;
        UsersInfo usersInfo = null;
        if(userObj!=null){
            usersInfo = (UsersInfo) userObj;      //转化对象
            userInfoId = usersInfo.getId();
        }
        cinemaCommentReply.setReplyuserId(userInfoId);   //设置回复表的用户id
        try {
            commentService.replyComment(cinemaCommentReply);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/movie/findMovieById?id="+movieId;
    }

    //商家回复评论(需要评论id和评论内容).回复完评论后需要修改评论表的flag，代表商家已回复了
    @RequestMapping("/replyCommentByCinema")
    @ResponseBody
    public JsonResult replyCommentByCinema(CinemaCommentReply cinemaCommentReply) {
        System.out.println(cinemaCommentReply);
        try {
            commentService.replyComment(cinemaCommentReply);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("评论失败");
        }
        return JsonResult.ok();
    }

    //商家删除评论
    @RequestMapping("deleteCommentByMovieId")
    @ResponseBody
    public JsonResult deleteCommentByMovieId(String movieId) {
        System.out.println("删除评论："+movieId);
        try {
            String[] movieids = movieId.split(",");
            Integer[] array = new Integer[movieids.length];
            for (int i = 0; i < array.length; i++) {
                array[i] = Integer.parseInt(movieids[i]);
            }
            commentService.deleteCommentByMovieId(array);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.ok("删除评论失败");
        }
        return JsonResult.ok("删除评论成功");
    }

}
